package dao;

import model.ImportModel;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import java.util.ArrayList;
import java.util.List;

public class ImportDao {

    public static List<ImportModel> getImportList() {
        List<ImportModel> list = new ArrayList<>();
        SessionFactory factory = new Configuration().configure().buildSessionFactory();
        Session session = factory.openSession();
        session.beginTransaction();
        String strSql = "select * from (select a.*,( SELECT b.business_id FROM tb_customer_import as b WHERE locate(b.customer_name,a.name) limit 1 ) as business_id from tb_customer_org as a ) as c where c.business_id is not null";
        Query query = session.createSQLQuery(strSql);
        List<Object[]> objects = query.list();
        if(objects!=null){
            for (Object[] object : objects) {
                ImportModel model=new ImportModel();
                model.setCompany(object[0].toString());
                model.setBusinessType(object[1].toString());
                model.setName(object[2].toString());
                model.setBusinessId(object[3].toString());
                list.add(model);
            }
        }
        return list;
    }
}
